#!/usr/bin/env python
##================
##Isometric Render example
##================
import pygame
import sys

import extensions
import lwpgt

SW, SH=640, 480
#main screen variable
SCREEN=pygame.display.set_mode((SW, SH))
#init pygame
pygame.init()
pygame.key.set_repeat(250, 50)
pygame.display.set_caption("2D Isometric Render Exmaple")

class isorenderexample01:
    """example class """
    def __init__(self):
        """init class"""
        self.drag=False
        #loading tiles
        self.DB=lwpgt.tools.ZipDB("data/tiles.zip")
        self.tile0=lwpgt.render2d.Tile(self.DB.GetImage("tile-iso.bmp"), (64, 32))
        self.tile1=lwpgt.render2d.Tile(self.DB.GetImage("tile1-iso.bmp"), (64, 32))
        self.herotile=lwpgt.render2d.Tile(self.DB.GetImage("hero-iso.bmp"), (64, 32))
        #create camera and map
        self.camera=lwpgt.render2d.Camera(pygame.Rect(0, 0, 320, 240))
        self.mapka=extensions.isorender.IsoRender()
        #setting up hero
        self.hero=lwpgt.render2d.Entity(self.herotile)
        self.hero.SetPosition(0, 0)
        self.hero.tile.SetAnimationDelay(0.100)
        
        #tiles list
        self.tiles=[self.tile0, self.tile1]
        #load map
        self.mapka.LoadMapFromXML("testmap.xml", self.tiles)
        
    ##update 
    def update(self):
        pass

    def redraw(self,screen):
        """redraw"""
        screen.fill((0,0,64))
        ##drawing here
        self.mapka.Render(SCREEN, self.camera)
        self.hero.Draw(SCREEN, self.camera)
        pygame.display.flip()
        
    
    def events(self):
        """events handler for example"""
        for e in pygame.event.get():
            if e.type==pygame.QUIT:
                sys.exit()
            if e.type==pygame.KEYDOWN:
                if e.key==pygame.K_LEFT:
                    self.hero.MoveObject(-32, -16)
                if e.key==pygame.K_RIGHT:
                    self.hero.MoveObject(32, 16)
                if e.key==pygame.K_UP:self.hero.MoveObject(32, -16)
                if e.key==pygame.K_DOWN:self.hero.MoveObject(-32, 16)
            #mouse moveing
            if e.type==pygame.MOUSEBUTTONDOWN:
                #set drag
                self.drag=True
            if e.type==pygame.MOUSEBUTTONUP:
                self.drag=False
            if e.type==pygame.MOUSEMOTION:
                if self.drag==True:self.camera.MoveCamera(e.rel[0], e.rel[1])
        #uncheck line under to turn on auto center camera on hero
        #self.camera.CenterAt(self.hero.GetScreenPosition())
    ##mainloop
    def mainloop(self):
        """main loop """
        while 1:
            self.events()
            self.update()
            self.redraw(SCREEN)
    
if __name__=="__main__":
    isorenderexample01().mainloop()
